package com.score.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.score.mapper.ICheckDormMapper;
import com.score.mapper.IDormMapper;
import com.score.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class StuInstructorServiceImpl {

    @Autowired
    private ICheckDormMapper checkDormMapper;

    @Autowired
    private IDormMapper dormMapper;


    public ReturnEntry getAllDormNum(String tung, String layer) {
        QueryWrapper<TDorm> dormQueryWrapper = new QueryWrapper<>();
        dormQueryWrapper.likeRight("dorm_num", tung + "-" + layer).groupBy("dorm_num");
        List<TDorm> tDorms = dormMapper.selectList(dormQueryWrapper);
        Set<String> collegeList = new HashSet<>(tDorms.size());
        for (int i = 0; i < tDorms.size(); i++) {
            collegeList.add(tDorms.get(i).getCollege());
        }
        return new ReturnEntry().ok(tDorms, collegeList);
    }

    public ResponseEntry insertCheckDorm(TCheckDorm checkDorm) {
        String chiPosition = Position.getChiPosition1(checkDorm.getChecker().substring(5));
        checkDorm.setChecker(chiPosition);
        QueryWrapper<TDorm> dormQueryWrapper = new QueryWrapper<>();
        dormQueryWrapper.eq("dorm_num", checkDorm.getDormNum());
        List<TDorm> dormList = dormMapper.selectList(dormQueryWrapper);
        Set<String> classSet = new HashSet<>();
        for (int i = 0; i < dormList.size(); i++) {
            dormList.get(i).setIscheck("check");
            classSet.add(dormList.get(i).getClassName());
            dormMapper.updateById(dormList.get(i));
        }
        int insert = checkDormMapper.insert(checkDorm);
        if (insert == 1) {
            return new ResponseEntry().ok("提交成功", insert);
        }
        return new ResponseEntry().fail("提交失败", insert);
    }

    public ReturnEntry getDormNum(String dormNum) {
        QueryWrapper<TDorm> dormQueryWrapper = new QueryWrapper<>();
        dormQueryWrapper.eq("dorm_num", dormNum);
        List<TDorm> dormList = dormMapper.selectList(dormQueryWrapper);
        return new ReturnEntry().ok(dormNum, "该寝室人员信息", dormList);
    }

    public ReturnEntry deletecheckdorm(Integer checkdormId) {
        int i = checkDormMapper.deleteById(checkdormId);
        if (i==1)return new ReturnEntry().ok(checkdormId+"",i);
        return new ReturnEntry().fail(checkdormId+"",i);
    }
}
